import time

import cv2
import mss
import numpy as np
import pyautogui
from pynput.keyboard import Controller as KC
from pynput.keyboard import Key
pyautogui.PAUSE = 0

r = r"C:\Users\Sinyer\Desktop"
class Screen:
    def __init__(self):
        self.cap = mss.mss()
        # left, top, width, height = pyautogui.getActiveWindow().box
        # left, top, right, bottom = left, top, left+width, top+height
        self.region = (360, 800, 1560, 1000)
        # self.region = (left, top, right, bottom)

        self.s = cv2.imread(rf"{r}/s.png")
        self.k = cv2.imread(rf"{r}/k.png")

    def _shot(self):
        img = self.cap.grab(self.region)
        img = np.resize(np.array(img.raw), (img.height, img.width, 4))  # BGRA四通道
        img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
        cv2.imshow("t", img)

        cv2.waitKey(0)
        cv2.destroyAllWindows()

    def main(self):
        s = self.shot()

    def shot(self, region):
        img = self.cap.grab(region)
        img = np.resize(np.array(img.raw), (img.height, img.width, 4))  # BGRA四通道
        return cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)

    def match(self, img1, img2):
        sift = cv2.SIFT_create()
        kp1, des1 = sift.detectAndCompute(img1, None)
        kp2, des2 = sift.detectAndCompute(img2, None)
        FLANN_INDEX_KDTREE = 1
        index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
        search_params = dict(checks=50)
        flann = cv2.FlannBasedMatcher(index_params, search_params)
        matches = flann.knnMatch(des1, des2, k=2)
        good_matches = []
        for m, n in matches:
            if m.distance < 0.7 * n.distance:
                good_matches.append(m)
        return len(good_matches) / len(matches)







Screen().shot()

# 360-1560 800-100
# 460 590 670 810 880 1020 1110 1240 1310 1460 1530
